﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using System.ServiceProcess;
using System.ServiceModel;
using Tesla.Service.ServiceImplementations;
using System.Net;

namespace Tesla.Service.WcfServiceHost
{
    class Program
    {
        static void Main(string[] args) 
        {

            foreach (string appKey in ConfigurationManager.AppSettings)
            {

                if (string.Compare(appKey, "GADNCommandTimeout") != 0)
                {
                    Uri baseAddress = new Uri(ConfigurationManager.AppSettings[appKey].Split(new char[1] { ';' })[0]);
                    ServiceHost ServHost = new ServiceHost(Type.GetType(ConfigurationManager.AppSettings[appKey].Split(new char[1] { ';' })[1]), baseAddress);


                    ServHost.Open();
                    Console.WriteLine("Tesla Service is running... Base Address: " + ConfigurationManager.AppSettings[appKey]);
                }
            }
            Console.ReadLine();            

            #region Old Code Keep this
            //Uri themeBaseAddress = new Uri(ConfigurationManager.AppSettings["SvcThemeNetTcp"]);
            //ServiceHost themeServHost = new ServiceHost(typeof(SvcTheme), themeBaseAddress);
            //themeServHost.Open();

            //Console.WriteLine("GADN Service is running...");
            //Console.ReadLine();


            //themeServHost.Close();



            //Uri employeeBaseAddress = new Uri(ConfigurationManager.AppSettings["SvcEmployeeNetTcp"]);
            //ServiceHost empServHost = new ServiceHost(typeof(SvcEmployee), employeeBaseAddress);
            //empServHost.Open();

            //Console.WriteLine("GADN Service is running...");
            //Console.ReadLine();


            //empServHost.Close();

            //if (Config.IsConsoleMode)
            //{
            //    foreach (var sc in Config.GetServiceConfigList())
            //    {
            //        Console.WriteLine(sc.Item.Key);
            //        Console.WriteLine("{0}: {1}", "HostTypeDeclaration", sc.Item.HostTypeDeclaration);
            //        Console.WriteLine("{0}: {1}", "HostTypeFullname", sc.Item.HostTypeFullname);
            //        Console.WriteLine("{0}: {1}", "HostTypeAssembly", sc.Item.HostTypeAssembly);
            //        Console.WriteLine("{0}: {1}", "ContractTypeDeclaration", sc.Item.ContractTypeDeclaration);
            //        Console.WriteLine("{0}: {1}", "ContractTypeFullname", sc.Item.ContractTypeFullname);
            //        Console.WriteLine("{0}: {1}", "ContractTypeAssembly", sc.Item.ContractTypeAssembly);
            //        Console.WriteLine("{0}: {1}", "ServiceAddressPort", sc.Item.ServiceAddressPort);
            //        Console.WriteLine("{0}: {1}", "EndpointName", sc.Item.EndpointName);
            //        Console.WriteLine("{0}: {1}", "AuthorizedGroups", sc.Item.AuthorizedGroups);
            //        Console.WriteLine("");
            //    }

            //    ServiceRunner sr = new ServiceRunner();
            //    sr.Start(args);
            //    Console.WriteLine("WcfServiceHost started... Hit enter to stop...");
            //    Console.ReadLine();
            //    sr.Stop();
            //}
            //else
            //{
            //    //Run it as a service
            //    ServiceBase[] servicesToRun;
            //    try
            //    {
            //        servicesToRun = new ServiceBase[] { new WcfService() };
            //        ServiceBase.Run(servicesToRun);
            //    }
            //    catch (Exception e)
            //    {
            //        //do your exception handling thing
            //        e.ProcessUnhandledException("WcfServiceHost");
            //    }
            //}

            #endregion
        }
    }
}
